package com.nsu.evaluation.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.controller.ClassController;
import com.nsu.evaluation.dao.*;
import com.nsu.evaluation.pojo.*;
import com.nsu.evaluation.pojo.Class;
import com.nsu.evaluation.service.RoleService;
import com.nsu.evaluation.utils.MD5Util;
import com.nsu.evaluation.utils.UUIDUtil;
import jdk.internal.dynalink.support.ClassMap;
import net.sf.jsqlparser.schema.Server;
import org.springframework.beans.factory.annotation.Autowired;

import com.nsu.evaluation.service.AdminUsersService;
import org.springframework.stereotype.Service;

@Service("adminUsersServiceImpl")
public class AdminUsersServiceImpl implements AdminUsersService{

	@Autowired
	private AdminUsersMapper adminUsersMapper;

	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private CourseMapper courseMapper;

	@Autowired
	private ClassMapper classMapper;

	@Autowired
	private StudentEvaluationsMapper studentEvaluationsMapper;

	@Autowired
	private StudentScoreMapper studentScoreMapper;

	@Autowired
	private RoleUserServiceImpl roleUserServiceImpl;

	@Autowired
	private ClassServiceImpl classServiceImpl;

	@Autowired
	private FeedbackReplyMapper feedbackReplyMapper;

	@Autowired
	private RoleServiceImpl roleServiceImpl;


	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public ServerResponse<AdminUsers> adminUserLogin(String username, String password) {
		AdminUsersExample adminUsersExample = new AdminUsersExample();
		AdminUsersExample.Criteria criteria = adminUsersExample.createCriteria();
		criteria.andUserNameEqualTo(username);
		long adminUserCount = adminUsersMapper.countByExample(adminUsersExample);
		if(adminUserCount == 0){
			return ServerResponse.createByErrorMessage("用户名不存在");
		}
		criteria.andPasswordEqualTo(password);
		List<AdminUsers> adminUsersList = adminUsersMapper.selectByExample(adminUsersExample);
		if(null != adminUsersList && adminUsersList.size()>0) {
			return ServerResponse.createBySuccess("登陆成功",adminUsersList.get(0));
		}else{
			return ServerResponse.createByErrorMessage("用户名或密码错误");
		}
	}

	/**
	 * 新增管理员用户
	 * @param data
	 * @return
	 */
	@Override
	public ServerResponse addAdminUsers(Map<String,String> data) {
		AdminUsers adminUsers = new AdminUsers();
		String adminUserId = UUIDUtil.getRandomUUID();
		adminUsers.setId(adminUserId);
		adminUsers.setUserName(data.get("username"));
		adminUsers.setPassword(MD5Util.MD5EncodeUtf8(data.get("password")));
		//创建对应的角色-用户
		RoleUser roleUser = new RoleUser();
		roleUser.setId(UUIDUtil.getRandomUUID());

		//获取到角色id
		Role role = new Role();
		role.setRoleName(data.get("roleType"));
		roleUser.setRoleId(roleServiceImpl.selectRoleByExample(role).getData().get(0).getId());
		roleUser.setUserId(adminUserId);
//		adminUsers.setPassword(MD5Util.MD5EncodeUtf8(adminUsers.getPassword()));
		//新增角色
		int rowCountUser = adminUsersMapper.insert(adminUsers);
		boolean rowCountRoleUser = roleUserServiceImpl.addRoleUser(roleUser).isSuccess();
		if(rowCountUser>0 && rowCountRoleUser){
			return ServerResponse.createBySuccess("创建用户及对应成功");
		}
		return ServerResponse.createByErrorMessage("创建用户失败");
	}

	/**
	 * 获取除入参指定用户以外的用户列表
	 * 根据用户id判断
	 * @param adminUsers
	 * @return
	 */
	@Override
	public ServerResponse<Map<String,Object>> getManagedAdminUsers(AdminUsers adminUsers){
		//创建AdminUserExample
		AdminUsersExample adminUsersExample = new AdminUsersExample();
		AdminUsersExample.Criteria criteria = adminUsersExample.createCriteria();
		criteria.andIdNotEqualTo(adminUsers.getId());

		//获取到除当前用户的所有用户
		List<AdminUsers> adminUsersList = adminUsersMapper.selectByExample(adminUsersExample);
		if(adminUsersList != null && adminUsersList.size()>0){
			//根据用户列表获取对应的权限列表
			List<String> adminUserRoleList = new ArrayList<>();
			for(AdminUsers adminUsersItem : adminUsersList){
				List<Role> roleList = roleUserServiceImpl.getRoleNameByUserId(adminUsersItem.getId()).getData();
				if(null != roleList){
					String roleName = roleList.get(0).getRoleName();
					adminUserRoleList.add(roleName);
				}
			}

			//获取到的数据保存在map中
			Map adminUserInfoMap = new HashMap();
			adminUserInfoMap.put("userlist",adminUsersList);
			adminUserInfoMap.put("rolelist",adminUserRoleList);
			//返回成功信息
			return ServerResponse.createBySuccess("获取列表成功",adminUserInfoMap);
		}
		return ServerResponse.createByErrorMessage("获取列表失败");
	}


	/**
	 * 根据ID更新用户信息
	 * @param adminUsers
	 * @return
	 */
	@Override
	public ServerResponse<AdminUsers> updateAdminUsersInfo(AdminUsers adminUsers){
		int updateCount = adminUsersMapper.updateByPrimaryKey(adminUsers);
		if(updateCount>0){
			return ServerResponse.createBySuccess("更新信息成功",adminUsers);
		}
		return ServerResponse.createByErrorMessage("更新信息失败");
	}

	/**
	 * 根据用户ID删除用户信息
	 * @param adminUserId
	 * @return
	 */
	@Override
	public ServerResponse deleteAdminUserByPrimaryKey(String adminUserId){
		int delCount = adminUsersMapper.deleteByPrimaryKey(adminUserId);
		if(delCount>0){
			return ServerResponse.createBySuccess("删除成功",adminUserId);
		}
		return ServerResponse.createByErrorMessage("删除失败");
	}

	/**
	 * 根据用户名进行查询
	 * @param adminUsers
	 * @return
	 */
	@Override
	public ServerResponse<List<AdminUsers>> queryAdminUserByUserName(AdminUsers adminUsers){
		//--创建一个查询Example
		AdminUsersExample adminUsersExample = new AdminUsersExample();
		//--取得Example的条件设置
		AdminUsersExample.Criteria criteria = adminUsersExample.createCriteria();
		//--设置查询名字等于谢轩璘的用户
		criteria.andUserNameEqualTo(adminUsers.getUserName());
		List<AdminUsers> list = adminUsersMapper.selectByExample(adminUsersExample);
		return ServerResponse.createBySuccess(list);
	}

	/**
	 * 根据用户ID进行查询
	 * @param adminUserId
	 * @return AdminUSers
	 */
	@Override
	public ServerResponse<AdminUsers> selectByPrimaryKey(String adminUserId){
		AdminUsers adminUsers = adminUsersMapper.selectByPrimaryKey(adminUserId);
		return ServerResponse.createBySuccess(adminUsers);
	}

	/**
	 * 获取用户列表
	 * @return
	 */
	@Override
	public ServerResponse<List<AdminUsers>> getAdminUsersList(){
		AdminUsersExample adminUsersExample = new AdminUsersExample();
		List<AdminUsers> adminUsersList =  adminUsersMapper.selectByExample(adminUsersExample);
		return ServerResponse.createBySuccess("获取列表成功",adminUsersList);
	}

	/**
	 * 新增学生用户
	 * @param student
	 * @return
	 */
	@Override
	public ServerResponse<Student> addStudentUser(Student student){
		//设置新学生用户的id和密码
		student.setUserPassword(MD5Util.MD5EncodeUtf8(student.getUserPassword()));
		String studentId = UUIDUtil.getRandomUUID();
		student.setId(studentId);
		//获取到角色信息
		Role role = new Role();
		role.setRoleName("学生用户");
		Role roleItem = roleServiceImpl.selectRoleByExample(role).getData().get(0);
		//新增角色-用户信息
		RoleUser roleUser = new RoleUser();
		roleUser.setId(UUIDUtil.getRandomUUID());
		roleUser.setRoleId(roleItem.getId());
		roleUser.setUserId(studentId);
		int studentCount = studentMapper.insert(student);
		boolean roleUserCount = roleUserServiceImpl.addRoleUser(roleUser).isSuccess();
		if(studentCount>0 && roleUserCount == true){
			return ServerResponse.createBySuccess("新增学生成功",student);
		}
		return ServerResponse.createByErrorMessage("新增学生失败");
	}

	/**
	 * 根据学生姓名或学号查询学生信息
	 * @param student
	 * @return
	 */
	@Override
	public ServerResponse<List<Student>> selectStudentByExample(Student student){
		StudentExample studentExample = new StudentExample();
		StudentExample.Criteria criteria = studentExample.createCriteria();
		if(student.getStudentId()!=null && !student.getStudentId().equals("")){
			criteria.andStudentIdEqualTo(student.getStudentId());
		}
		if(student.getIdCardName()!=null && !student.getIdCardName().equals("")){
			criteria.andIdCardNameEqualTo(student.getIdCardName());
		}
		List<Student> studentList = studentMapper.selectByExample(studentExample);
		return ServerResponse.createBySuccess("查询学生信息成功",studentList);
	}

	/**
	 * 根据ID删除学生信息
	 * @param studentId
	 * @return
	 */
	@Override
	public ServerResponse deleteStudentByPrimaryKey(String studentId){
		int rowCount = studentMapper.deleteByPrimaryKey(studentId);
		if(rowCount>0){
			return ServerResponse.createBySuccess("删除学生信息成功",studentId);
		}
		return ServerResponse.createByErrorMessage("删除学生信息失败");
	}

	/**
	 * 修改学生信息
	 * @param student
	 * @return
	 */
	@Override
	public ServerResponse<Student> updateStudentByPrimaryKey(Student student) {
		int rowCount = studentMapper.updateByPrimaryKey(student);
		if(rowCount>0){
			return ServerResponse.createBySuccess("修改学生信息成功",student);
		}
		return ServerResponse.createByErrorMessage("修改学生信息失败");
	}

	/**
	 * 获取全部学生列表
	 * @return
	 */
	@Override
	public ServerResponse<List<Student>> getStudentList(){
		StudentExample studentExample = new StudentExample();
		List<Student> studentList = studentMapper.selectByExample(studentExample);
		List<Student> studentRes = new ArrayList<>();
		for (Student student : studentList){
			//如果学生存在班级id，则将班级id替换成班级名称
			if(student.getClassId()!=null && !student.getClassId().equals("")) {
				ServerResponse response = classServiceImpl.getClassNameById(student.getClassId());
				if (response.isSuccess()) {
					String className = response.getData().toString();
					student.setClassId(className);
				}
			}
			studentRes.add(student);
		}
		return ServerResponse.createBySuccess("获取列表成功",studentRes);
	}

}
